<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="方式" prop="checkType">
        <el-select v-model="queryParams.checkType" filterable placeholder="请选择方式">
          <el-option v-for="dict in dict.type.sys_check_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-cascader :props="{ value: 'id', checkStrictly: true }" :show-all-levels="false" ref="reference"
          placeholder="请选择部门" :style="{ width: '100%' }" v-model="queryParams.deptId"
          :options="deptOptions"></el-cascader>
      </el-form-item>

      <el-form-item label="日期">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cultivate:checkRecord:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['cultivate:checkRecord:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['cultivate:checkRecord:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cultivate:checkRecord:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="printBox"
          v-hasPermi="['equipment:checkRecord:print']">打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table @selection-change="handleSelectionChange" border fit v-loading="loading" :data="dataList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" />
      <el-table-column show-overflow-tooltip label="考核方式">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_check_type" :value="scope.row.checkType" />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="评价">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_check_result" :value="scope.row.result" />
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="部门" prop="deptName" />
      <el-table-column show-overflow-tooltip label="考核时间" prop="checkDate" />
      <el-table-column show-overflow-tooltip label="考核人" prop="checkUserName" />
      <el-table-column show-overflow-tooltip label="被考核人" prop="checkedUserName" />
      <el-table-column show-overflow-tooltip label="平均分" prop="averagePoint" />

      <el-table-column show-overflow-tooltip label="内容" prop="content" />
      <el-table-column show-overflow-tooltip label="创建时间" min-width="180" prop="createTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['cultivate:checkRecord:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cultivate:checkRecord:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cultivate:checkRecord:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 打印模板位置 -->
    <PrintBox :printRow="printList" checkLabel="checkLabel" :isVertical="false" 
      id="printBox" :paddrg="80">
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 40px">
          试验室人员考核评价记录表 STET/B/14-5
        </div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table style="border-collapse: collapse; table-layout: fixed; width: 90%" border="1" cellspacing="0"
          class="table-page" :isVertical="false">
          <tr class="table_header" style="height: 20mm;">
            <td style="width: 40mm;font-size: 20px" >部门</td>
            <td style="width: 80mm;font-size: 20px">{{ scope.item.deptName }}</td>
            <td style="width: 40mm;font-size: 20px">考核时间</td>
            <td style="width: 80mm;font-size: 20px">
              {{ scope.item.checkDate }}
            </td>
          </tr>
          <tr style="height: 20mm">
            <td style="font-size: 20px;">考核内容</td>
            <td colspan="3" style="font-size: 20px;">{{ scope.item.content }}</td>
          </tr>
          <tr>
            <td rowspan="2" style="font-size: 20px;">考核方式</td>
            <td rowspan="2" style="font-size: 20px;">
              <div style="display: flex; align-items: center">
                <div v-for="item in dict.type.sys_check_type" style="display: flex; align-items: center; width: 20mm">
                  <input type="checkbox" :checked="item.value == scope.item.checkType" />{{ item.label }}
                </div>
              </div>
            </td>
            <td style="font-size: 20px;"> 考核人</td>
            <td style="font-size: 20px;">
              {{ scope.item.checkUserName }}
            </td>
          </tr>
          <tr>
            <td style="font-size: 20px;">被考核人</td>
            <td style="font-size: 20px;">
              {{ scope.item.checkedUserName }}
            </td>
          </tr>
          <tr>
            <td style="font-size: 20px;">考核过程
            </td>
            <td colspan="3" style="text-align: left; line-height: 10mm">

                <div v-for="(item,index) in scope.item.contentList" style="display: flex; align-items: start;font-size: 23px;">
                  <input type="checkbox" style="margin-top: 10px" :checked="item.point?true:false" />
                  <div><span style="font-weight: bold;white-space: nowrap;" v-if="item.sort == 1">{{ item.type=='1'?'规范：':item.type=='2'?'设备：':item.type=='3'?'试验过程：':'数据处理：' }}</span>{{ item.content }}</div>
                  <div v-if="item.point">
                    （<span>{{ item.point || "" }}</span>）
                  </div>
                </div>
            </td>
          </tr>
          <tr style="font-size: 20px;">
            <td style="font-size: 20px;">考核结果评价</td>
            <td colspan="3" style="text-align: left; height: 80mm;font-size: 20px;">
              <div style="font-size: 20px;">最终得分（平均值）（{{ scope.item.averagePoint }}）</div>
              <div style="display: flex">
                <div style="font-size: 20px;">该试验人员最终考核结果评价：</div>
                <div>
                  <div v-for="item in dict.type.sys_check_result" style="display: flex; align-items: center;font-size: 20px" >
                    {{ item.label }}
                    <input type="checkbox" :checked="item.value == scope.item.result" />
                  </div>
                </div>
              </div>
              <div>注：任意一项考核或最终评分70分以下，均为考核不通过。</div>
            </td>
          </tr>
        </table>
        <!-- 底部信息位置 -->
        <div class="desc">注：附考核资料</div>
        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import {
  listCheckAppraiseRecord,
  delCheckAppraiseRecord,
} from "@/api/cultivate/checkRecord.js";
import { deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";

export default {
  name: "checkRecord",
  dicts: ["sys_check_type", "sys_check_result"],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkType: undefined,
        deptId: [],
        licenseNum: undefined,
        checkDate: null,
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
      infos: [], //勾选的数据
      printList: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startEndDate) {
        this.dateRange.push(this.queryParams.startEndDate, this.queryParams.endEndDate)
      }
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      console.log(this.printList,'this.printList');
      setTimeout(() => {
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          padding: 4px;
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.checkAppraiseRecordId);
      this.single = selection.length != 1;
      this.infos = selection;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.checkAppraiseRecordId || this.ids;
      this.$router.push("./addCheckRecord?isEdit=true&id=" + configId);
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push(
        "./addCheckRecord?isInfo=true&id=" + row.checkAppraiseRecordId
      );
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.checkAppraiseRecordId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "checkAppraiseRecordId",
        "DELETE",
        "checkedUserName"
      );

      this.$modal
        .confirm('是否确认删除被考核人为"' + name + '"的数据项？')
        .then(function () {
          return delCheckAppraiseRecord(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addCheckRecord");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCheckAppraiseRecord({
        ...this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        ),
        deptId: this.queryParams.deptId.slice(-1)[0],
      }).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.download(
        "system/checkAppraiseRecord/export",
        {
          ...this.queryParams,
        },
        `实验室人员考核评价记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
